package com.common.library.widget;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.library.util.ActivityHelper;
import com.common.library.util.Helper;

/**
 * 
 * function: 自定义listview
 * 
 * @ author: aXiBa
 */
public class SDKGameListView extends LinearLayout {
	
	ArrayList<?> data;
	
	ItemHandler mItemHandler;

	public SDKGameListView(Context context) {
		super(context);
	}
	
	public SDKGameListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}



	/**
	 * 
	 * function: 初始化
	 *
	 * @param data
	 * @param itemHandler
	 * 
	 * @ author: aXiBa 2013-3-20 下午2:53:42
	 */
	public void init(ArrayList<?> data, ItemHandler itemHandler) {
		this.data = data;
		this.mItemHandler = itemHandler;
		
		LayoutInflater inflater = LayoutInflater.from(getContext());
		
		// 初始化view
		setOrientation(LinearLayout.VERTICAL);
		
		if(Helper.isNotEmpty(data)) {
			for (int i = 0; i < data.size(); i++) {
				
				View item = inflater.inflate(ActivityHelper.getLayoutResId("zs4gamelayout_view_listitem"), null);
				
				final int pos = i;
				
				TextView mContentView = (TextView) item.findViewById(ActivityHelper.getIdResId("zs4gameviewid_mContentView_view_listitem"));
				TextView mInfoView = (TextView) item.findViewById(ActivityHelper.getIdResId("zs4gameviewid_mInfoView_view_listitem"));
				ImageView mIconView = (ImageView) item.findViewById(ActivityHelper.getIdResId("zs4gameviewid_mIconView_view_listitem"));
				
				View mSlideView = (View) item.findViewById(ActivityHelper.getIdResId("zs4gameviewid_mSlideView_view_listitem"));
				if(null != mItemHandler) {
					mContentView.setText(Html.fromHtml(mItemHandler.getItemContent(pos)));
					
					String info = mItemHandler.getItemInfo(pos);
					if(Helper.isNotEmpty(info)) {
						mInfoView.setText(info);
						mInfoView.setVisibility(View.VISIBLE);
						mIconView.setVisibility(View.GONE);
					}
					else {
						mInfoView.setVisibility(View.GONE);
						mIconView.setVisibility(View.VISIBLE);
					}
					
					item.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							mItemHandler.onItemClick(pos);
						}
					});
					
				}
				
				// 设置背景
				if(1 == data.size()) {
					mSlideView.setVisibility(View.INVISIBLE);
				}
				else if(data.size()>1) {
					
					if(pos < data.size()-1) {
						mSlideView.setVisibility(View.VISIBLE);
					}
					else {
						mSlideView.setVisibility(View.INVISIBLE);
					}

				}
				
				addView(item);
			}
		}
	}
	
	
	/**
	 * 
	 * function: 得到item数据
	 *
	 * @param pos
	 * @return
	 * 
	 * @ author: aXiBa 2013-3-20 下午2:50:52
	 */
	public Object getItem(int pos) {
		return (null!=data && pos<data.size()) ? data.get(pos) : null;
	}
	
	/**
	 * 
	 * function: 得到大小
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-3-20 下午2:51:01
	 */
	public int getSize() {
		return null!=data ? data.size() : 0;
	}
	
	
	/**
	 * 
	 * function: 
	 * 
	 * @ author: aXiBa
	 */
	public interface ItemHandler {
		
		/**
		 * 
		 * function: 每项显示的内容
		 *
		 * @param pos
		 * @return
		 * 
		 * @ author: aXiBa 2013-3-20 下午3:05:57
		 */
		String getItemContent(int pos);
		
		/**
		 * 
		 * function: 每项右侧信息,如果没有显示箭头
		 *
		 * @param pos
		 * @return
		 * 
		 * @ author: aXiBa 2013-4-2 下午5:07:53
		 */
		String getItemInfo(int pos);
		
		/**
		 * 
		 * function: 每项点击处理
		 *
		 * @param pos
		 * 
		 * @ author: aXiBa 2013-3-20 下午3:06:09
		 */
		void onItemClick(int pos);
	}
}
